package com.ymatou.iapi.idcardmng.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardmng.parameter.ReceiveMobileFromTradingBean;
import com.ymatou.iapi.idcardmng.service.ReceiveMobileFromTradingCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_ReceiveMobileFromTrading {
	
	private static ReceiveMobileFromTradingBean bean;
	private static ReceiveMobileFromTradingCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("接收交易提供的手机号码");
	}

	@Before
	public void caseUp() {
		bean = new ReceiveMobileFromTradingBean();
		call = new ReceiveMobileFromTradingCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verifyDb() throws Exception {

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;
		String oldKey = "1234567890123456";
		String newKey = "123456789";

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		YmtIdCardInfo expectedIdCardInfo = ymtIdCard.selectByNameAndBuyerId(
				bean.getName(), bean.getBuyerId());

		Logger.comment("校验name:" + expectedIdCardInfo.getName());
		
		if (expectedIdCardInfo.getPhone() != null) 
			Logger.verifyEquals(
					CipherUtil.decryptAES(oldKey, expectedIdCardInfo.getPhone()),
					bean.getMobile(), "校验phone");
		
		if (expectedIdCardInfo.getTempPhone() != null) 
			Logger.verifyEquals(
					expectedIdCardInfo.getTempPhone(),
					bean.getMobile(), "校验tempPhone");
		
		Logger.verifyEquals(
				expectedIdCardInfo.getMobile(),
				CipherUtil.encryptAES(newKey, bean.getMobile()), "校验mobile");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveMobileFromTrading_001() {
		Logger.start(true, "根据买家的姓名和userid修改手机号码");
		try {
			
			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;
			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByUserId(buyerId, 1, true, 0).get(0);

			bean.setBuyerId(buyerId);
			bean.setMobile(TestDataManager.getPhoneNum());
			bean.setName(ymtIdCardInfo.getName());

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ok", call.getCode(), "验证Code");
			
			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
